//
//  UITabBarItem+WZLBadge.m
//  WZLBadgeDemo
//
//  Created by zilin_weng on 15/9/24.
//  Copyright (c) 2015年 Weng-Zilin. All rights reserved.
//

#import "UITabBarItem+WZLBadge.h"

#define kActualView                     [self getActualBadgeSuperView]

@implementation UITabBarItem (WZLBadge)

#pragma mark -- public methods

/**
 *  show badge with red dot style and WBadgeAnimTypeNone by default.
 */
- (void)showBadge
{
    [kActualView showBadge];
}

/**
 *  showBadge
 *
 *  @param style WBadgeStyle type
 *  @param value (if 'style' is WBadgeStyleRedDot or WBadgeStyleNew,
 this value will be ignored. In this case, any value will be ok.)
 *   @param aniType
 */
- (void)showBadgeWithStyle:(WBadgeStyle)style
                     value:(NSInteger)value
             animationType:(WBadgeAnimType)aniType
{
    [kActualView showBadgeWithStyle:style value:value animationType:aniType];
}

// 设置WBadgeStyleRectangle 类型
- (void)showRectangleBadgeWithValue:(NSString *)value {
    [kActualView showRectangleBadgeWithValue:value];
}

/// 设置WBadgeStyleRectangle
/// @param style 默认UIRectCornerAll
- (void)showRectangleBadgeWithCorner:(UIRectCorner)style value:(NSString *)value {
    [kActualView showRectangleBadgeWithCorner:style value:value];
}

/**
 *  clear badge
 */
- (void)clearBadge
{
    [kActualView clearBadge];
}

#pragma mark -- private method

/**
 *  Because UIBarButtonItem is kind of NSObject, it is not able to directly attach badge.
 This method is used to find actual view (non-nil) inside UIBarButtonItem instance.
 *
 *  @return view
 */
- (UIView *)getActualBadgeSuperView
{
    UIView *tabbarButton = [self valueForKeyPath:@"_view"];
    UIView *badgeView = [tabbarButton.superview viewWithTag:kBadgeViewBasicTab+self.tag];
    if (!badgeView) {
        badgeView = [[UIView alloc] initWithFrame:tabbarButton.frame];
        badgeView.backgroundColor = [UIColor clearColor];
        badgeView.tag = kBadgeViewBasicTab+self.tag;
        badgeView.userInteractionEnabled = NO;
        [tabbarButton.superview addSubview:badgeView];
    }
    return badgeView;//use KVC to hack actual view
}

#pragma mark -- setter/getter
- (UILabel *)badge
{
    return kActualView.badge;
}

- (void)setBadge:(UILabel *)label
{
    [kActualView setBadge:(BadgeLabel *)label];
}

- (UIColor *)badgeBgColor
{
    return [kActualView badgeBgColor];
}

- (void)setBadgeBgColor:(UIColor *)badgeBgColor
{
    [kActualView setBadgeBgColor:badgeBgColor];
}

- (UIColor *)badgeTextColor
{
    return [kActualView badgeTextColor];
}

- (void)setBadgeTextColor:(UIColor *)badgeTextColor
{
    [kActualView setBadgeTextColor:badgeTextColor];
}

- (WBadgeAnimType)aniType
{
    return [kActualView aniType];
}

- (void)setAniType:(WBadgeAnimType)aniType
{
    [kActualView setAniType:aniType];
}

- (CGRect)badgeFrame
{
    return [kActualView badgeFrame];
}

- (void)setBadgeFrame:(CGRect)badgeFrame
{
    [kActualView setBadgeFrame:badgeFrame];
}

- (CGPoint)badgeCenterOffset
{
    return [kActualView badgeCenterOffset];
}

- (void)setBadgeCenterOffset:(CGPoint)badgeCenterOffset
{
    [kActualView setBadgeCenterOffset:badgeCenterOffset];
}

- (void)showNumberBadgeWithValue:(NSInteger)value{
}

@end
